package k8s

import (
	"context"

	batchv1 "k8s.io/api/batch/v1"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	batch "k8s.io/client-go/kubernetes/typed/batch/v1"
)

func NewJobs(cli *kubernetes.Clientset) *Jobs {
	return &Jobs{
		core: cli.BatchV1(),
	}
}

type Jobs struct {
	core batch.BatchV1Interface
}

// 获取Job列表
func (s *Jobs) GetJobs(ctx context.Context, namespace string, req *v1.ListOptions) (*batchv1.JobList, error) {
	return s.core.Jobs(namespace).List(ctx, *req)
}

// 获取Job详情
func (s *Jobs) GetJob(ctx context.Context, namespace, name string) (*batchv1.Job, error) {
	return s.core.Jobs(namespace).Get(ctx, name, v1.GetOptions{})
}

// 删除Job
func (s *Jobs) DeleteJob(ctx context.Context, namespace, name string) error {
	return s.core.Jobs(namespace).Delete(ctx, name, v1.DeleteOptions{})
}

// 创建Job
func (s *Jobs) CreateJob(ctx context.Context, namespace string, job *batchv1.Job) (*batchv1.Job, error) {
	return s.core.Jobs(namespace).Create(ctx, job, v1.CreateOptions{})
}

// 更新Job
func (s *Jobs) UpdateJob(ctx context.Context, namespace string, job *batchv1.Job) (*batchv1.Job, error) {
	return s.core.Jobs(namespace).Update(ctx, job, v1.UpdateOptions{})
}
